import { App, DirectiveBinding } from "vue";

export const regDebounce = (app: App<Element>) => {
  app.directive("debounce", {
    mounted(el: HTMLElement, binding: DirectiveBinding) {
      let timer: NodeJS.Timeout | null = null;

      // 事件监听器点击
      el.addEventListener("click", () => {
        // 清除定时器
        if (timer) clearTimeout(timer);
        // 开启定时器
        timer = setTimeout(() => {
          binding.value();
        }, 700);
      });
    },
  });
};
